package com.drcoffee.drcoffeemall.mapper;

import com.drcoffee.drcoffeemall.entry.AfterSale;
import com.drcoffee.drcoffeemall.entry.Order;
import com.drcoffee.drcoffeemall.entry.OrderAttachment;
import com.drcoffee.drcoffeemall.entry.Trade;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Mapper
@Repository
public interface AfterSaleMapper {

    @Select("SELECT b.*,c.part_name, CONCAT(d.o_file_path, d.folder, c.image_url) as file_path " +
            "FROM t_after_m_order_part a " +
            "LEFT JOIN  t_order_attachment b ON a.order_part_code = b.part_code " +
            "LEFT JOIN t_attachment c on b.part_code = c.part_code,t_file_path d " +
            "WHERE a.after_code = #{code} AND b.order_code = #{order_code} AND c.language = 'CN' AND d.bsh_one = 'G' AND d.bsh_two = 1")
    List<OrderAttachment> getAfterParts(String code, String order_code);

    @Select("SELECT CONCAT(o_file_path, folder) as path FROM t_file_path WHERE bsh_one = 'K' AND bsh_two = 1")
    String getAfterBeforePath();


    @Update("UPDATE t_after_sale SET status = #{status}${sql}#{username} WHERE code = #{code} AND status_two = 0")
    void setAfterSaleStatus(String code, Integer status, String sql, String username);

    @Update("UPDATE t_after_sale SET remark = #{remark}, status_two = #{statusTwo}${sql}#{username} WHERE code =#{code} AND status_two = 0")
    void setAfterSaleStatusTwo(String code, Integer statusTwo, String remark, String sql, String username);

    @Select("SELECT * FROM t_after_sale WHERE code = #{code}")
    AfterSale getAfterSaleBycode(String code);

    @Select("SELECT * FROM t_trade WHERE trade_type = 'I' AND order_code = #{code}")
    Trade getPayTradeByAfter(String code);

    @Insert("INSERT INTO t_trade VALUES (#{id},#{order_code},#{trade_date},#{trade_no},#{refund_id},#{trade_amount},#{trade_type},#{trade_platform})")
    void insertTrade(Trade trade);

    @Select("SELECT recipients,phone,address FROM t_order WHERE code = #{orderCode}")
    Order getOrderAddress(String orderCode);

    @Update("UPDATE t_after_sale SET status = 5,transport_code_bf = #{trackingCode},time_out=DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%s')  WHERE code = #{code} AND status = 4")
    void enterReissue(String code, String trackingCode);

    @Update("UPDATE t_after_sale SET status = #{status} WHERE code = #{code}")
    void updateAfterSaleStatus(AfterSale afterSale);

    @Select("SELECT * FROM t_after_sale")
    List<AfterSale> allAfterSale();

    //    @Select("SELECT * FROM t_after_sale WHERE status = #{status} AND status_two = 0")
    //    Map<String, Object> getNoExamineAfterSale(Map<String, Object> paramMap);
    //    @Select("SELECT * FROM t_after_sale WHERE type=#{type} AND status = #{status} AND status_two = 0")


    List<Map<String, Object>> getAfterSaleByStatus(Map<String, Object> paramMap);

    @Select("SELECT COUNT(*) FROM t_trade WHERE order_code = #{out_trade_no} and trade_type = #{trade_type}")
    Integer getTradeNum(String out_trade_no, String trade_type);

}
